const http = require('http');

const nodes = [
  { name: 'Seed', port: 8081 },
  { name: 'Regulator', port: 8083 },
  { name: 'Exchange', port: 8084 },
  { name: 'Enterprise1', port: 8085 },
  { name: 'Enterprise2', port: 8086 }
];

function checkNode(node) {
  return new Promise((resolve) => {
    const req = http.get(`http://localhost:${node.port}/chaininfo`, (res) => {
      let data = '';
      res.on('data', chunk => data += chunk);
      res.on('end', () => {
        console.log(`✅ ${node.name} (${node.port}): Running`);
        resolve(true);
      });
    });

    req.on('error', (err) => {
      console.log(`❌ ${node.name} (${node.port}): ${err.code || 'Not responding'}`);
      resolve(false);
    });

    req.setTimeout(3000, () => {
      console.log(`⏰ ${node.name} (${node.port}): Timeout`);
      req.destroy();
      resolve(false);
    });
  });
}

async function checkAllNodes() {
  console.log('🔍 Checking RepChain nodes...\n');
  
  const results = await Promise.all(nodes.map(checkNode));
  const runningCount = results.filter(r => r).length;
  
  console.log(`\n📊 Summary: ${runningCount}/${nodes.length} nodes running`);
  
  if (runningCount === 0) {
    console.log('\n💡 To start nodes, run: start-nodes-cmd.bat');
  } else if (runningCount < nodes.length) {
    console.log('\n⚠️  Some nodes are not running. Check the individual node windows.');
  } else {
    console.log('\n🎉 All nodes are running!');
  }
}

checkAllNodes(); 